from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from apps.contents.models import GoodsCategory, GoodsChannel
from apps.goods.models import SKU, SPU, SPUSpecification
from meiduo_mall_admin.meiduo_admin.serializers import SkuInfoSerializer, PagNun, SkuCategoriesSerializer, \
  SpuSpecsSerializer


# -----------SKU管理--------
class SKUInfoView(ModelViewSet):

  serializer_class = SkuInfoSerializer

  pagination_class = PagNun

  def get_queryset(self):


    keyword = self.request.query_params.get('keyword')

    if keyword == '' or keyword is None:
      queryset = SKU.objects.all()
    else:
      queryset = SKU.objects.filter(name__contains=keyword)
    return queryset



class SkuCategoriesView(ListAPIView):
  # queryset = GoodsCategory.objects.filter(parent=None)
  queryset = GoodsCategory.objects.filter(subs=None)

  serializer_class = SkuCategoriesSerializer


class SpuSimpleView(ListAPIView):
  queryset = SPU.objects.all()
  serializer_class = SkuCategoriesSerializer


class SpuSpecsView(ListAPIView):
  serializer_class = SpuSpecsSerializer

  def get_queryset(self):
    pk = self.kwargs['pk']
    queryset = SPUSpecification.objects.filter(spu_id=pk)
    return queryset
